<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="900px">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="老人姓名" prop="elderId">
            <el-input
              v-model="formData.elder.name"
              placeholder="请输入老人姓名"
              disabled
              class="mr-1"
            />
            <el-button @click="clickChooseElder" v-if="isEdit">选择老人</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="来访者姓名" prop="visitName">
            <el-input
              v-model="formData.visitName"
              :placeholder="isEdit ? '请输入来访者姓名' : ''"
              :clearable="true"
              :disabled="!isEdit"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="来访者手机" prop="visitMobile">
            <el-input
              v-model="formData.visitMobile"
              :placeholder="isEdit ? '请输入来访者手机' : ''"
              :clearable="true"
              :disabled="!isEdit"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="与老人关系" prop="kinship">
            <el-input
              v-model="formData.kinship"
              :placeholder="isEdit ? '请输入与老人关系' : ''"
              :disabled="!isEdit"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="来访时间" prop="visitTime">
            <el-date-picker
              v-model="formData.visitTime"
              type="datetime"
              :placeholder="isEdit ? '请输入来访时间' : ''"
              :clearable="true"
              value-format="YYYY-MM-DD HH:mm:ss"
              date-format="MMM DD, YYYY"
              time-format="HH:mm"
              :disabled="!isEdit"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="同行人数" prop="visitCount">
            <InputNumber v-model="formData.visitCount" :clearable="true" :disabled="!isEdit" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="来访缘由" prop="visitReason">
            <el-input
              v-model="formData.visitReason"
              :placeholder="isEdit ? '请输入来访缘由' : ''"
              :clearable="true"
              type="textarea"
              :rows="3"
              :disabled="!isEdit"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="formData.remark"
              :placeholder="isEdit ? '请输入备注' : ''"
              :clearable="true"
              type="textarea"
              :rows="3"
              :disabled="!isEdit"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
  <ElderCheckInSelect ref="checkInElderSelectRef" @select-value="handleRelatedElder" />
</template>
<script setup name="ElderVisitEdit" lang="ts">
import * as ElderVisitApi from '@/api/elder/visit'
import * as ElderVistModel from '@/model/elder/register/ElderVisit'
import { ElderVisitStatusEnum } from '@/utils/constants'
import ElderCheckInSelect from '@/views/elder/elderCheckIn/ElderCheckInSelect.vue'
import { ElderCheckInStatusEnum } from '@/utils/constants'
const checkInElderSelectRef = ref()
const isEdit = ref(true)
const { t } = useI18n()
const message = useMessage()
let formType = ''
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const formData = ref({
  elder: { name: '' },
  elderId: '',
  visitName: '',
  visitMobile: '',
  kinship: '',
  visitTime: undefined,
  visitCount: 0,
  visitReason: '',
  remark: '',
  status: ElderVisitStatusEnum.RESERVED
})
const formRules = reactive({
  elderId: [{ required: true, message: '请输入老人姓名', trigger: 'blur' }],
  visitName: [{ required: true, message: '请输入来访者姓名', trigger: 'blur' }],
  visitTime: [{ required: true, message: '请输入来访时间', trigger: 'blur' }]
})
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ElderVisitApi.singleGet(id)
      isEdit.value = formData.value.status == ElderVisitStatusEnum.RESERVED
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    elder: { name: '' },
    elderId: '',
    visitName: '',
    visitMobile: '',
    kinship: '',
    visitTime: undefined,
    visitCount: 0,
    visitReason: '',
    remark: '',
    status: ElderVisitStatusEnum.RESERVED
  }
  formRef.value?.resetFields()
  isEdit.value = true
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return

  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ElderVistModel.ElderVisit
    if (formType === 'create') {
      await ElderVisitApi.create(data)
      message.success(t('common.createSuccess'))
    } else {
      await ElderVisitApi.update(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
const clickChooseElder = () => {
  checkInElderSelectRef.value.open({ status: { value: ElderCheckInStatusEnum.CHECKED_IN } })
}

const handleRelatedElder = async (row) => {
  formData.value.elderId = row.elderId
  formData.value.elder.name = row.elder.name
}
</script>
